import os
from PIL import Image,ImageFont,ImageDraw

class Draw:
    BG_PATH = './images/'
    WHIRE = (255,255,255)
    BLUE = (0, 51, 153)
    YELLOW = (255,246,0)
    BLACK =(0,0,0)
    def __init__(self):
        #设置文件背景
        self.bj = './images/bj.jpg'
        self.txt = ('布丁考研', '真题 答案 笔记', '习题集', '中国科学院大学', '2022版', '考研复习资料', '(涵盖考研真题及笔记)', '科大科院考研网 独家提供', '2022中科院',
                    '好评入V群\n复试答疑\n全年无忧', '爆款\n推荐')
        #创建文件目录
        self.__drawing()
    def __drawing(self):
        if os.path.exists('./title.txt') :
            with open('./title.txt','r+',encoding='utf-8') as file :
                title_list = file.readlines()
                for title in title_list :
                    title =  title.replace('\n','').strip()
                    with Image.open(self.bj) as im :
                        im_drow = ImageDraw.Draw(im)
                        #标题
                        self.__default(im_drow,title)
                        #im.show()
                        im.save(f'{self.BG_PATH}{title}.png')

        else :
            os.mkdir('title.txt')

    #写字
    @staticmethod
    def __text(im_drow,pos,text,fill,font):
        im_drow.multiline_text(pos, text, fill=fill, font=font)
        return im_drow

    # 设置字体
    @staticmethod
    def __font(size):
        return ImageFont.truetype('C:\\Windows\\FONTS\\SIMKAI.ttf', size)

    #居中
    @staticmethod
    def __center(big,samll):
        x,y = (big[2]-samll[0])//2+big[0],(big[3]-samll[1])//2+big[1]
        return x,y

    #全局默认文字
    def __default(self,im_drow,title):
        txt = self.txt
        logo = txt[0]
        tip1 = txt[1]
        tip2 = txt[2]
        h1 = txt[3]
        version = txt[4]
        h2 = f'《{title}》' + txt[5]
        h3 = txt[6]
        h4 = txt[7]
        h5 = txt[8]
        left_h1 = txt[9]
        hot = txt[10]
        font = Draw.__font(50)
        pos = Draw.__center((260, 600, 440, 80), font.getsize(title))
        Draw.__text(im_drow, pos, title, Draw.BLUE, font)
        # logo
        font = Draw.__font(50);
        pos = Draw.__center((11, 11, 275, 80), font.getsize(logo))
        Draw.__text(im_drow, pos, logo, Draw.BLUE, font)
        # tip
        fonttp1 = Draw.__font(30)
        fonttp1y = 33
        Draw.__text(im_drow, (365, fonttp1y), tip1, Draw.WHIRE, fonttp1)

        # tip2
        font = Draw.__font(30)
        Draw.__text(im_drow, (375 + fonttp1.getsize(tip1)[0], fonttp1y), tip2, Draw.YELLOW, font)

        # h1
        font = Draw.__font(35)
        h1x = 300
        h1y = 162
        Draw.__text(im_drow, (h1x, h1y), h1, Draw.BLACK, font)

        # version
        font = Draw.__font(25)
        Draw.__text(im_drow, (h1x + 80, h1y + 50), version, Draw.BLACK, font)

        # h2
        font = Draw.__font(15)
        Draw.__text(im_drow, (h1x, h1y + 50 + 30), h2, Draw.BLACK, font)
        # h3
        font = Draw.__font(15);
        Draw.__text(im_drow, (h1x + 50, h1y + 50 + 30 * 2), h3, Draw.BLACK, font)

        # h4
        font = Draw.__font(15);
        Draw.__text(im_drow, (h1x + 35, h1y + 50 + 30 * 3), h4, Draw.BLACK, font)

        # h5
        font = Draw.__font(32);
        Draw.__text(im_drow, (30, 550), h5, Draw.BLUE, font)

        # h5
        font = Draw.__font(32);
        h5y = 550
        Draw.__text(im_drow, (30, h5y), h5, Draw.BLUE, font)

        # h5
        font = Draw.__font(31);
        Draw.__text(im_drow, (40, h5y + 40), left_h1, Draw.YELLOW, font)

        # hot
        font = Draw.__font(25);
        Draw.__text(im_drow, (588, 440), hot, Draw.WHIRE, font)

if __name__ == '__main__' :
    Draw()